function WindowList( ) {
	this.highestBoxZIndex = 300;
}

WindowList.prototype.Add = function(obj) {
	obj.borderBounded = true;

	obj.addEventListener('mousedown', function(evt) {
		// if the id is the parent thing then do it, otherwise ignore?
		if(!window.opera) {
			this.clickedX = evt.layerX;
			this.clickedY = evt.layerY;
			obj.moving = true;
			obj.style.zIndex = ++this.highestBoxZIndex;
		}
	}, false);

	obj.addEventListener('mouseup', function(evt) {
		obj.moving = false;
	}, false);

	obj.addEventListener('mousemove', function(evt) {
		if(obj.moving) {
			MoveBox(evt, obj, obj.clickedX, obj.clickedY);
		}
	}, false);

	if(this.Data) {
		this.Data[this.Data.length] = obj;
	}
	else {
		this.Data = new Array(1);
		this.Data[0] = obj;
	}
}

function MoveBox(evt, i, boxX, boxY) {
	var activeEvent = evt ? evt : window.event;

	var borderBounded = i.borderBounded;

	var data = '';

	var offsetX = 0;
	var offsetY = 0;
	
	var screenWidth;
	var screenHeight;

	var x;
	var y;

	var pageOffsetY;
	var IEFix = 0;

	// I hate Opera...
	if(window.opera) {
		x = evt.clientX;
		y = evt.clientY;
		
		screenWidth = window.innerWidth;
		screenHeight = window.innerHeight;

		pageOffsetY = window.pageYOffset;

	}
	else if(window.addEventListener) {
		x = evt.pageX;
		y = evt.pageY;

		screenWidth = window.innerWidth;
		screenHeight = window.innerHeight;

		pageOffsetY = window.pageYOffset;
	}
	else {
		x = activeEvent.x;
		y = activeEvent.y;

		screenWidth = document.documentElement.clientWidth || document.body.clientWidth;
		screenHeight = document.documentElement.clientHeight || document.body.clientHeight;

		pageOffsetY = document.body.scrollTop || document.documentElement.scrollTop;
	
		IEFix = pageOffsetY;
	}
	
	if(borderBounded && x + i.offsetWidth > screenWidth) {
		offsetX = (screenWidth - (boxX)) - i.offsetWidth;
	}
	else {
		offsetX = x - boxX;
	}

	if(borderBounded && y + i.offsetHeight + IEFix > (screenHeight + pageOffsetY)) {
		offsetY = (screenHeight + pageOffsetY - (boxY)) - i.offsetHeight;
	}
	else {
		offsetY = y + IEFix - boxY;
	}

	offsetY -= 30;

	i.style.left = offsetX + 'px';
	i.style.top = offsetY + 'px';

	if(navigator.appName.indexOf('JavaScript Debug System') > -1) {
		data = '';
		data += '(x - boxX)' + (x - boxX) + '<br/>\n';
		data += '(x, y) (' + (x) + ', ' + (y) + ')<br/>\n';
		data += '(offsetX, offsetY) (' + (offsetX) + ', ' + (offsetY) + ')<br/>\n';
		i.innerHTML = data;
	}
};

function AddMovement(obj) {
	var windowList;

	if(window.addEventListener && !window.opera) {
		windowList = new WindowList( );
		windowList.Add(obj);
	}

	else if(window.attachEvent) {
		obj.moving = false;
		obj.attachEvent('onclick', function(evt) {
			obj.clickedX = evt.offsetX;
			obj.clickedY = evt.offsetY;
			if(obj.moving) {
				obj.moving = false;
			}
			else {
				obj.moving = true;
			}

			document.attachEvent('onmousemove', function(evt) {
				if(obj.moving) {
					MoveBox(evt, obj, obj.clickedX, obj.clickedY);
				}
			});

			window.event.cancelBubble = true;
			window.event.returnValue = false;
		});
	}
}